package com.vilen.weixin.Media;

import com.vilen.weixin.json.WeixinMedia;
import com.vilen.weixin.model.WeixinFinalValue;
import com.vilen.weixin.util.JsonUtil;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2016/2/10.
 */
public class MediaKit {
    public static String postMedia(String path,String type) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            String url = WeixinFinalValue.POST_MEDIA;
            url = url.replace("ACCESS_TOKEN", WeixinFinalValue.TEMP_AT);
            url = url.replace("TYPE",type);
            HttpPost post = new HttpPost(url);
            FileBody fb = new FileBody(new File(path));
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("media",fb).build();
            post.setEntity(reqEntity);
            response = client.execute(post);
            int sc = response.getStatusLine().getStatusCode();
            if(sc>=200&& sc<300) {
                String json = EntityUtils.toString(response.getEntity());
                WeixinMedia wm = (WeixinMedia) JsonUtil.getInstance().json2obj(json,WeixinMedia.class);
                return wm.getMedia_id();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client!=null) {
                    client.close();
                }
                if(response!=null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void getMedia(String mediaId, File f) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        try {
            client = HttpClients.createDefault();
            String url = WeixinFinalValue.GET_MEDIA;
            url = url.replace("ACCESS_TOKEN", WeixinFinalValue.TEMP_AT);
            url = url.replace("MEDIA_ID",mediaId);
            HttpGet get = new HttpGet(url);
            response = client.execute(get);
            int sc = response.getStatusLine().getStatusCode();
            if(sc>=200&& sc<300) {
                InputStream is = response.getEntity().getContent();
                FileUtils.copyInputStreamToFile(is,f);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client!=null) {
                    client.close();
                }
                if(response!=null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
